<?php


namespace App\Models;


use App\System\traits\Paginatable;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * App\Models\Customer
 *
 * @property int $id
 * @property int $create_id 创建用户
 * @property int $follow_id 跟进人
 * @property string $contact_name 客户姓名
 * @property string $contact_mobile 联系电话
 * @property string $company_name 公司名称
 * @property string $company_position 客户职位
 * @property string $wechat 客户微信
 * @property int $from 客户来源
 * @property int $level 客户级别
 * @property int $province 省份ID
 * @property int $city 城市ID
 * @property int $district 城市ID
 * @property string $address 公司地址
 * @property int $status 是否有效
 * @property int $in_sea 是否在公海里
 * @property int $assigned 是否分配
 * @property Carbon $assigned_at 添加微信时间
 * @property string $remark 备注
 * @property Carbon $add_wechat_at 添加微信时间
 * @property \Illuminate\Support\Carbon|null $created_at
 * @property \Illuminate\Support\Carbon|null $updated_at
 * @method static \Illuminate\Database\Eloquent\Builder|Customer newModelQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Customer newQuery()
 * @method static \Illuminate\Database\Eloquent\Builder|Customer query()
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereAddWechatAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereAddress($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereAssigned($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereCity($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereCompanyName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereCompanyPosition($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereContactMobile($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereContactName($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereCreateId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereCreatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereFollowId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereFrom($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereId($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereInSea($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereLevel($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereProvince($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereRemark($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereStatus($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereUpdatedAt($value)
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereWechat($value)
 * @mixin \Eloquent
 * @property-read \App\Models\User $createdUser
 * @property-read \App\Models\User $followUser
 * @method static \Illuminate\Database\Eloquent\Builder|Customer whereDistrict($value)
 * @property-read \App\Models\District $cityData
 * @property-read \App\Models\District $districtData
 * @property-read \App\Models\District $provinceData
 */
class Customer extends Model
{
    use Paginatable;

    // 客户无效状态
    const STATUS_INVALID = 5;
    // 客户正常状态
    const STATUS_NORMAL = 10;

    // 客户级别 A
    const LEVEL_A = 1;
    // 客户级别 B
    const LEVEL_B = 2;
    // 客户级别 C
    const LEVEL_C = 3;
    // 客户级别 D
    const LEVEL_D = 4;

    // 客户来源400电话
    const FROM_400 = 1;
    // 客户来源老朋友介绍
    const FROM_INTRODUCE = 2;
    // 客户来源 朋友介绍
    const FROM_FRIEND = 3;
    // 客户来源 头条
    const FROM_TT = 4;
    // 客户开源 抖音
    const FROM_DY = 5;
    // 客户来源 快手
    const FROM_KS = 6;
    // 客户来源陌拜
    const FROM_MB = 7;
    // 客户来源电话
    const FROM_MOBILE = 8;
    // 客户来源展会
    const FROM_ZH = 9;
    // 客户来源 公众号
    const FROM_GZH = 10;
    // 客户来源 官网
    const FROM_GW = 11;

    public static $FROM_VALUES = [
        self::FROM_400 => '400电话',
        self::FROM_INTRODUCE => '老朋友介绍',
        self::FROM_FRIEND => '朋友介绍',
        self::FROM_TT => '今日头条',
        self::FROM_DY => '抖音',
        self::FROM_KS => '快手',
        self::FROM_MB => '陌拜',
        self::FROM_MOBILE => '电话',
        self::FROM_ZH => '展会',
        self::FROM_GZH => '公众号',
        self::FROM_GW => '官网',
    ];

    protected $table = 'customer';

    protected $casts = [
        'add_wechat_at' => 'datetime',
        'assigned_at' => 'datetime',
    ];

    protected $fillable = ['contact_name', 'contact_mobile', 'company_name', 'company_position', 'company_desc', 'wechat', 'from', 'level', 'status', 'province', 'city', 'district', 'address', 'remark'];

    public function getLevelText()
    {
        switch ($this->level) {
            case static::LEVEL_A:
                return 'A类';
            case static::LEVEL_B:
                return 'B类';
            case static::LEVEL_C:
                return 'C类';
            case static::LEVEL_D:
                return 'D类';
        }
        return '';
    }

    public function getStatusText()
    {
        switch ($this->status) {
            case static::STATUS_NORMAL:
                return '正常';
            case static::STATUS_INVALID:
                return '无效';
        }
        return '';
    }

    public function getFromText()
    {
        switch ($this->from) {
            case static::FROM_400:
                return '400电话';
            case static::FROM_INTRODUCE:
                return '老客户介绍';
            case static::FROM_FRIEND:
                return '朋友介绍';
            case static::FROM_TT:
                return '今日头条';
            case static::FROM_DY:
                return '抖音';
            case static::FROM_KS:
                return '快手';
            case static::FROM_MB:
                return '陌拜';
            case static::FROM_MOBILE:
                return '电话';
            case static::FROM_ZH:
                return '展会';
            case static::FROM_GZH:
                return '公众号';
            case static::FROM_GW:
                return '官网';
        }
        return '';
    }

    /**
     * 根据汉字反向获取值
     * @param $string
     * @return int|string
     * @author thanatos thanatos915@163.com
     */
    public static function getFromValue($string)
    {
        $index = 0;
        foreach (static::$FROM_VALUES as $key => $item) {
            if ($item == $string) {
                $index = $key;
                break;
            }
        }
        return $index;
    }


    public function createdUser()
    {
        return $this->belongsTo(User::class, 'create_id');
    }

    public function followUser()
    {
        return $this->belongsTo(User::class, 'follow_id');
    }

    public function provinceData()
    {
        return $this->belongsTo(District::class, 'province');
    }

    public function cityData()
    {
        return $this->belongsTo(District::class, 'city');
    }

    public function districtData()
    {
        return $this->belongsTo(District::class, 'district');
    }

}
